package cn.mily.basics.utils;

import cn.mily.basics.exception.UtilException;
import org.apache.commons.httpclient.Header;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;


public class SendMsg_webchinese {

    /*请求资源*/
    private static PostMethod post = null;
    /*发送信息的id*/
    private static final String UID = "mily_EMT";
    /*发送信息的键*/
    private static final String KEY = "d41d8cd98f00b204e980";

    /**
     * 网建发送验证码短信工具类
     * @param phone 需要发送的手机号
     * @param verCode 要发送的内容
     */
    public static void sendVerCodeMsg(String phone,String verCode) throws UtilException {
        try {
            //创建Http客户端
            HttpClient client = new HttpClient();
            //创建一个post方法
            post = new PostMethod("http://utf8.api.smschinese.cn");
            //添加请求头信息
            post.addRequestHeader("Content-Type",
                    "application/x-www-form-urlencoded;charset=utf8");//在头文件中设置转码
            NameValuePair[] data = {new NameValuePair("Uid", UID),
                    new NameValuePair("Key", KEY),
                    new NameValuePair("smsMob", phone),
                    new NameValuePair("smsText", verCode)};
            //设置请求体
            post.setRequestBody(data);
            //执行post方法
            client.executeMethod(post);
            //获取响应头信息
            Header[] headers = post.getResponseHeaders();
            //获取状态码
            int statusCode = post.getStatusCode();
            String result = new String(post.getResponseBodyAsString().getBytes("utf8"));
            System.out.println(result); //打印返回消息状态
        } catch (Exception e) {
            throw new UtilException("抱歉！验证码发送失败！");
        } finally {/*关闭请求资源流*/
            post.releaseConnection();
        }
    }
}
